<?php
define('IN_PHPBB', true);
define('BB_ROOT', './');
require(BB_ROOT ."common.php");

// Start session management
$user->session_start();

// Check if user logged in
if (!$userdata['session_logged_in'])
{
    redirect(append_sid("login.php?redirect=index.php", true));
}

// GET's
$mode = (isset($_POST['mode'])) ? $_POST['mode'] : '';
$topic_id = (isset($_POST['t']) && is_numeric($_POST['t'])) ? $_POST['t'] : '0';
$user_id = $userdata['user_id'];


// Mods
if ($mode == 'add_thx' && $userdata['user_id'] != ANONYMOUS) {

       $sql="SELECT topic_poster FROM bb_topics WHERE topic_id = '$topic_id'";
        if( !($result = DB()->sql_query($sql)) ){
                message_die(GENERAL_ERROR, "Could not query topic", '', __LINE__, __FILE__, $sql);
        }
        $row = DB()->sql_fetchrow($result);
        $ch_myself = $row['topic_poster'];
       if ($user_id == $ch_myself) {
          
           $message = '<center>Сам себя не похвалишь - никто не похвалит xD</center><br />' . sprintf($lang['CLICK_RETURN_TOPIC'], '<a href="viewtopic.php?t='.$topic_id.'">', '</a>');
           message_die(GENERAL_MESSAGE, $message);
       }

	$sql="SELECT user_id FROM bb_thanks WHERE topic_id = '$topic_id' AND user_id = '$user_id'";
        if( !($result = DB()->sql_query($sql)) ){
                message_die(GENERAL_ERROR, "Could not query topic", '', __LINE__, __FILE__, $sql);
        }
        $row = DB()->sql_fetchrow($result);
        $ch_user = $row['user_id'];
	if ($user_id == $ch_user) {
	   
	    $message = '<center><i><b>Вы уже благодарили</b></i></center><br />' . sprintf($lang['CLICK_RETURN_TOPIC'], '<a href="viewtopic.php?t='.$topic_id.'">', '</a>');
	    message_die(GENERAL_MESSAGE, $message);
	}
    
    $sql = "INSERT INTO bb_thanks (topic_id, user_id) values (". $topic_id .",". $userdata['user_id'] .")";
    DB()->sql_query($sql);

 

   
    $message = '<center>Спасибо за "Спасибо"!</center><br />' . sprintf($lang['CLICK_RETURN_TOPIC'], '<a href="viewtopic.php?t='.$topic_id.'">', '</a>');
    message_die(GENERAL_MESSAGE, $message);
}


if (!$mode) {
    $message = '<center>Access denied!</center><br /><a href="javascript:history.go(-1)">Вернуться назад</a>';
    message_die(GENERAL_MESSAGE, $message);
}


?>
